/**
 * 文件：PhotoItem.java
 * 包名：com.ronri.choosephotos;   --- 正式发布时需要修正
 * 说明：单张照片信息
 * 作者：MS During
 * 日期：2017/5/12
 * 历史：
 */
package com.ronri.choosephotos;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import com.ronri.common.FileManager;

import java.util.Date;


public class PhotoItem {

    // 照片ID
    public int ID = 0;

    // 照片路径
    public String PhotoPath = "";

    // 经度
    public double Latitude = 0.0;

    // 维度
    public double Longitude = 0.0;

    // 地址
    public String Address = "";

    // 拍照时间
    public Date PhotoDate = new Date();

    // 服务器地址
    public String Url = "";

    // 是否已经上传
    public boolean isUpload = false;

    // 是否为操作对象
    public boolean isOperator = false;

    // 备注
    public String Memo = "";

    public PhotoItem() {
        super();
    }

    public PhotoItem(boolean operator) {
        super();
        isOperator = operator;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getPhotoPath() {
        return PhotoPath;
    }

    public void setPhotoPath(String photoPath) {
        PhotoPath = photoPath;
    }

    public double getLatitude() {
        return Latitude;
    }

    public void setLatitude(double latitude) {
        Latitude = latitude;
    }

    public double getLongitude() {
        return Longitude;
    }

    public void setLongitude(double longitude) {
        Longitude = longitude;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    public Date getPhotoDate() {
        return PhotoDate;
    }

    public void setPhotoDate(Date photoDate) {
        PhotoDate = photoDate;
    }

    public String getUrl() {
        return Url;
    }

    public void setUrl(String url) {
        Url = url;
    }

    public boolean isUpload() {
        return isUpload;
    }

    public void setUpload(boolean upload) {
        isUpload = upload;
    }

    public boolean isOperator() {
        return isOperator;
    }

    public void setOperator(boolean operator) {
        isOperator = operator;
    }

    public String getMemo() {
        return Memo;
    }

    public void setMemo(String memo) {
        Memo = memo;
    }

    /**
     * 根据信息获取照片
     * @return
     */
    public Bitmap GetBitmap(){
        // 判断文件是否存在
        if(!FileManager.FileExists(PhotoPath)) {
            return null;
        }

        Bitmap cameraBitmap = BitmapFactory.decodeFile(PhotoPath);
        /**
         * 把图片旋转为正的方向
         */
        return cameraBitmap;//BitmapManager.rotaingImageView(90, cameraBitmap);
    }

    /***
     * 根据照片信息获取指定大小图片
     * @param nWidth
     * @param nHeight
     * @return
     */
    public Bitmap GetSmallBitmap(int nWidth, int nHeight){

        // 判断文件是否存在
        if(!FileManager.FileExists(PhotoPath)) {
            return null;
        }

        Bitmap bitmap = BitmapFactory.decodeFile(PhotoPath);

        //获取这个图片的宽和高
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        //定义预转换成的图片的宽度和高度
        int newWidth = nWidth;
        int newHeight = nHeight;

        //计算缩放率，新尺寸除原始尺寸
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();

        // 缩放图片动作
        matrix.postScale(scaleWidth, scaleHeight);

        //旋转图片 动作
        // matrix.postRotate(90);

        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                width, height, matrix, true);

        if(bitmap != null)
            if(!bitmap.isRecycled())
                bitmap.recycle();

        return resizedBitmap;

//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;
//        options.inPreferredConfig = Bitmap.Config.ARGB_4444;
//        options.inPurgeable = true;
//        options.inInputShareable = true;
//        options.outWidth = 100;
//        options.outHeight = 100;
//
//        Bitmap cameraBitmap = BitmapFactory.decodeFile(PhotoPath, options);
        /**
         * 把图片旋转为正的方向
         */
        //return cameraBitmap;// BitmapManager.rotaingImageView(90, cameraBitmap);
    }
}
